package com.sskj.functions.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.common.base.App;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.functions.R;
import com.sskj.functions.R2;
import com.sskj.functions.presenter.CurrentRecordPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CurrentRecordBean;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;

/**
 * @author Hey
 * Create at  2019/07/05
 */
@Route(path = RConfig.FUNCTIONS_CURRENT_RECORD)
public class CurrentRecordActivity extends BaseActivity<CurrentRecordPresenter> {
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.rvRecord)
    RecyclerView rvRecord;
    @BindView(R2.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;
    private int page = 1;
    private int size = 10;
    private BaseAdapter<CurrentRecordBean.ListBean> adapter;
    private CurrentRecordBean data;

    @Override
    public int getLayoutId() {
        return R.layout.functions_activity_current_record;
    }

    @Override
    public CurrentRecordPresenter getPresenter() {
        return new CurrentRecordPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText(App.INSTANCE.getString(R.string.functions_currentRecordActivity1));
        mPresenter.getRecord(page, size);
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getRecord(page, size);
        });
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getRecord(page, size);
        });
    }

    @Override
    public void initData() {
        rvRecord.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseAdapter<CurrentRecordBean.ListBean>(R.layout.current_record_item, null, rvRecord, true) {
            @Override
            public void bind(ViewHolder holder, CurrentRecordBean.ListBean item) {
                String coinName = "";
                if (item.getPtype().contains("_")) {
                    coinName = item.getPtype().substring(0, item.getPtype().indexOf("_"));
                }
                holder.setText(R.id.tvName, item.getMemo())
                        .setText(R.id.tvTime, TimeFormatUtil.formatE(Long.valueOf(item.getAddtime()) * 1000))
                        .setText(R.id.tvType, item.getPrice() + " " + coinName.toUpperCase());
            }
        };
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, CurrentRecordActivity.class);
        context.startActivity(intent);
    }

    @SuppressLint("CheckResult")
    public void getRecordSuccess(CurrentRecordBean data) {
        if (data == null) {
            return;
        }
        if (data.getList() == null) {
            return;
        }
        if (page == 1) {
            adapter.setNewData(data.getList());
        } else {
            adapter.addData(data.getList());
        }
    }

    public void onStoRefresh() {
        if (smartRefreshView != null) {
            smartRefreshView.finishRefresh();
            smartRefreshView.finishLoadMore();
        }
    }
}
